package cf

import (
	"gopkg.in/yaml.v2"
	"os"
)

type MConfig struct {
	MySqlConfig MQConfig  `yaml:"mysql"`
	LogConfig   LogConfig `yaml:"log"`
	Back        Back      `yaml:"back"`
}

type MQConfig struct {
	Host     string `yaml:"host"`
	Username string `yaml:"username"`
	Password string `yaml:"password"`
	Port     string `yaml:"port"`
	Db       string `yaml:"db"`
}

type LogConfig struct {
	Level      string `yaml:"level"`
	Filename   string `yaml:"filename"`
	MaxSize    int    `yaml:"max_size"`
	MaxAge     int    `yaml:"max_age"`
	MaxBackups int    `yaml:"max_backups"`
}

type Back struct {
	BackDir           string `yaml:"back_dir"`
	ZapDir            string `yaml:"zap_dir"`
	Count             int    `yaml:"count"`
	MaxGoroutineCount int    `yaml:"max_goroutine_count"`
	Timer             string `yaml:"timer"`
	FileName          string `yaml:"file_name"`
	Password          string `yaml:"password"`
}

var Config *MConfig

func Init(path string) error {
	conf := &MConfig{}
	if f, err := os.Open(path); err != nil {
		return err
	} else {
		yaml.NewDecoder(f).Decode(conf)
	}

	Config = conf
	return nil
}
